import useUserStore from "@/store/user";
import axios from "axios";

const request = axios.create({
  baseURL: process.env.VUE_APP_API_BASE_URL,
  timeout: 10000,
});

const useStore = useUserStore();

request.interceptors.request.use(
  (config) => {
    const token = useStore.token;
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    // if (error.response.status === 401) {
    //   useStore.logout();
    // }
    return Promise.reject(error);
  }
);

export default request;
